

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;

import com.hogofwar.DJbot.BasePlugin;
import com.hogofwar.DJbot.IRCBase;

public class COM_down implements BasePlugin {

	public void start(final IRCBase irc, final String[] extra, String channel,
			String sender, String hostname, boolean usename) {
		// System.out.println(extra[0]);
		// System.out.println(extra[1]);
		Boolean online = false;
		String start = "";
		if (usename == true) {
			start = sender + ": ";
		}
		// System.out.println("not"+ extra[0]);

		if (extra.length != 0) {
			if (extra[0] != " ") {
				System.out.println("here");
				InetAddress address = null;
				String add = null;
				if (!extra[0].contains("http://")) {
					add = "http://" + extra[0];
				} else {
					add = extra[0];
				}
				String tosend = null;
				if (exists(add)) {
					// address.getCanonicalHostName()
					tosend = add + " seems to be up.";
				} else {
					tosend = add + " seems to be down.";
				}

			
				irc.sendMessage(channel, start + "" + tosend);
			} else {
				
				irc.sendMessage(channel, start
						+ "You need to specify a website!");
			}
		} else {
			
			irc.sendMessage(channel, start + "You need to specify a website!");
		}

	}

	public String help() {
		return "Down - down [website] - Check if a website is down or not.";
		// TODO Auto-generated method stub

	}

	public static boolean exists(String URLName) {
		try {
			HttpURLConnection.setFollowRedirects(true);
			// note : you may also need
			// HttpURLConnection.setInstanceFollowRedirects(false)
			HttpURLConnection con = (HttpURLConnection) new URL(URLName)
					.openConnection();
			con.setRequestMethod("HEAD");
			return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public String version() {
		return "Down version 1";
		// TODO Auto-generated method stub

	}

	@Override
	public String restrict() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean hide() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String aliases() {
		// TODO Auto-generated method stub
		return "isup, check";
	}

}
